package com.tank.tank;

import java.util.Vector;

public class EnemyTank extends Tank implements Runnable {
    private int step = 30;
    //保存敌人坦克信息
    Vector<EnemyTank> enemyTanks = new Vector<>();

    public void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        this.enemyTanks = enemyTanks;
    }

    public boolean isTouchEnemyTank() {
        switch (getDirect()) {
            case 0://向上走
                //取出所有坦克比较
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //不能和自己比
                    if (this != enemyTank) {
                        //1对面坦克竖着
                        //左上角
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //对面坦克坐标范围
                            //X:[enemtyTank.getX(),enemtyTank.getX()+40]
                            //Y:[enemtyTank.getY(),enemtyTank.getY()+60]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <=enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                            //右上角
                            if (this.getX() +40>= enemyTank.getX()
                                    &&this.getX()+40 <= enemyTank.getX() + 40
                                    && this.getY()>=enemyTank.getY()
                                    &&this.getY()<= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        //2对面坦克横着
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            //对面坦克坐标范围
                            //X:[enemtyTank.getX(),enemtyTank.getX()+60]
                            //Y:[enemtyTank.getY(),enemtyTank.getY()+40]
                            //左上角
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY()>= enemyTank.getY()
                                    && this.getY()<= enemyTank.getY() + 40) {
                                return true;
                            }
                            //右上角
                            if (this.getX()+40 >= enemyTank.getX()
                                    && this.getX()+40 <= enemyTank.getX() + 60
                                    && this.getY()>=enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
            case 1://向右走
                //取出所有坦克比较
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //不能和自己比
                    if (this != enemyTank) {
                        //1对面坦克竖着
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //对面坦克坐标范围
                            //X:[enemtyTank.getX(),enemtyTank.getX()+40]
                            //Y:[enemtyTank.getY(),enemtyTank.getY()+60]
                            //右上角
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX()+ 60 <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY()<= enemyTank.getY() + 60) {
                                return true;
                            }
                            //右下角
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <=enemyTank.getX() + 40
                                    && this.getY()+40 >= enemyTank.getY()
                                    && this.getY()+40<= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        //2对面坦克横着
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            //对面坦克坐标范围
                            //X:[enemtyTank.getX(),enemtyTank.getX()+60]
                            //Y:[enemtyTank.getY(),enemtyTank.getY()+40]
                            //右上
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX()+ 60 <= enemyTank.getX() + 60
                                    && this.getY()>= enemyTank.getY()
                                    && this.getY() <=enemyTank.getY() + 40) {
                                return true;
                            }
                            //右下
                            if (this.getX()+ 60 >= enemyTank.getX()
                                    && this.getX()+ 60 <= enemyTank.getX() + 60
                                    && this.getY()+40 >= enemyTank.getY()
                                    && this.getY()+40 <=enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
            case 2://向下走
                //取出所有坦克比较
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //不能和自己比
                    if (this != enemyTank) {
                        //1对面坦克竖着
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //对面坦克坐标范围
                            //X:[enemtyTank.getX(),enemtyTank.getX()+40]
                            //Y:[enemtyTank.getY(),enemtyTank.getY()+60]
                            //左下
                            if (this.getX()>= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() + 60 >= enemyTank.getY()
                                    &&this.getY()+ 60 <= enemyTank.getY() + 60) {
                                return true;
                            }
                            //右下
                            if (this.getX()+40 >= enemyTank.getX()
                                    && this.getX()+40  <= enemyTank.getX() + 40
                                    && this.getY()+ 60 >= enemyTank.getY()
                                    && this.getY()+ 60 <= enemyTank.getY() + 60) {
                                return true;
                            }

                        }
                        //2对面坦克横着
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            //对面坦克坐标范围
                            //X:[enemtyTank.getX(),enemtyTank.getX()+60]
                            //Y:[enemtyTank.getY(),enemtyTank.getY()+40]
                            if (this.getX()>= enemyTank.getX()
                                    && this.getX()<= enemyTank.getX() + 60
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <=enemyTank.getY() + 40) {
                                return true;
                            }
                            if (this.getX()+40 >= enemyTank.getX()
                                    && this.getX()+40 <= enemyTank.getX() + 60
                                    && this.getY()+ 60 >= enemyTank.getY()
                                    && this.getY()+ 60 <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
            case 3://向左走
                //取出所有坦克比较
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //不能和自己比
                    if (this != enemyTank) {
                        //1对面坦克竖着
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //对面坦克坐标范围
                            //X:[enemtyTank.getX(),enemtyTank.getX()+40]
                            //Y:[enemtyTank.getY(),enemtyTank.getY()+60]
                            //左上
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                            //左下
                            if (this.getX()>= enemyTank.getX()
                                    && this.getX()<= enemyTank.getX() + 40
                                    && this.getY()+40 >= enemyTank.getY()
                                    && this.getY()+40 <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        //2对面坦克横着
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            //对面坦克坐标范围
                            //X:[enemtyTank.getX(),enemtyTank.getX()+60]
                            //Y:[enemtyTank.getY(),enemtyTank.getY()+40]
                            if (this.getX() >= enemyTank.getX()
                                    &&this.getX()<= enemyTank.getX() + 60
                                    &&this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX()<= enemyTank.getX() + 60
                                    && this.getY()+40 >= enemyTank.getY()
                                    && this.getY()+40 <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
        }
        return false;
    }

    //敌人坦克子弹用vector保存多个子弹
    Vector<Shot> shots=new Vector<>();
    public EnemyTank(int x, int y) {
        super(x, y);
    }

    @Override
    public void run() {//敌人坦克动起来
        while(isLive()) {
            //发射子弹
            if(isLive()&&shots.size()<1){
               Shot shot=null;
               //判断坦克方向,创建子弹
                switch (getDirect()){
                    case 0:
                        shot=new Shot(getX()+20,getY()-10,0);
                        break;
                    case 1:
                        shot =new Shot(getX()+70,getY()+20,1);
                        break;
                    case 2:
                        shot=new Shot(getX()+20,getY()+70,2);
                        break;
                    case 3:
                        shot=new Shot(getX()-10,getY()+20,3);
                        break;
                }
                shots.add(shot);
                new Thread(shot).start();
            }

            //坦克移动
            switch (getDirect()) {
                case 0://上
                    for (int i = 0; i < step; i++) {
                        if(getY()>0&&!isTouchEnemyTank()) {
                            moveUp();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    break;
                case 1:
                    for (int i = 0; i < step; i++) {
                        if(getX()+60<1000&&!isTouchEnemyTank()) {
                            moveRight();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < step; i++) {
                        if(getY()+60<750&&!isTouchEnemyTank()) {
                            moveDown();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    break;
                case 3:
                    for (int i = 0; i < step; i++) {
                        if(getX()>0&&!isTouchEnemyTank()) {
                            moveLeft();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    break;
            }
            //判断是否活着
//            if(!isLive){
//                System.out.println("我挂了!!!!");
//                break;
//            }
            //移动后随机改变坦克方向
            setDirect((int) (Math.random() * 4));

        }
    }
}
